<template>
  <div>
    <header class="home-header wrap"
            :class="{'active' : headerActive}">
      <i class="iconfont icon-caidan"></i>
      <div class="header-search">
        <span class="app-name">G</span>
        <i class="iconfont icon-search"></i>
        <span class="search-title">家电返场同价11.11</span>
      </div>
      <span>登录</span>
    </header>
    <!-- <header :class="['home-header', 'wrap', { 'header-active': headerActive }]">
      <i class="iconfont icon-caidan"></i>
      <div class="header-search">
        <span class="app-name">G</span>
        <i class="iconfont iconfont-wxbsousuotuiguang"></i>
        <span class="search-title">家电返场同价11.11</span>
      </div> -->
    <!-- ==================== -->
    <!-- 判断是否登录 显示登录成功图标 -->
    <!-- v-if="!$store.getters.getIsLogin" -->
    <!-- ==================== -->
    <!-- <router-link tag="span"
                   to="/login">登录</router-link>
      <router-link class=""
                   tag="i"
                   to="/me"> </router-link>
</header> -->
    <!-- =================== -->
    <!-- 不建议 这么跳转 -->
    <!-- <span v-if="!$store.getters.getIsLogin" @click="$router.push('/login')"
        >登录</span
      > -->
    <!-- <i class="icon-me iconfont iconfont-gerenzhongxin" v-else @click="$router.push('/me')"></i> -->

    <!-- 首页轮播 -->
    <van-swipe :autoplay="3000"
               indicator-color="white"
               class="swiper-container">
      <van-swipe-item v-for="item in sliders"
                      :key="item.id">
        <van-image v-bind:src="item.imgUrl"> </van-image>
      </van-swipe-item>
    </van-swipe>

    <!-- 分类 -->
    <section class="category-list">
      <!-- 不建议 在 v-for 中 为 key 值绑定 索引-->
      <div v-for="(item, index) in categoryList"
           :key="index"
           @click="$router.push('/product-list')">
        <img :src="item.imgUrl" />
        <span>{{ item.name }}</span>
      </div>
    </section>

    <!-- 商品 楼梯 推荐项目 -->
    <section class="floor-list">
      <div class="floor-wrap"
           v-for="proItem in floorList"
           :key="proItem.headUrl">
        <img :src="proItem.headUrl"
             class="floor-head" />
        <div class="floor-content">
          <div class="floor-category"
               v-for="item in proItem.list"
               :key="item.title">
            <p>{{ item.title }}</p>
            <p>{{ item.desc }}</p>
            <div class="floor-products">
              <img v-for="(imgs, index) in item.products"
                   :key="index"
                   :src="imgs.imgUrl" />
            </div>
          </div>
        </div>
      </div>
    </section>

    <tabbar :active="0" />
  </div>
</template>

<script>
// 导入 自定义组件
import tabbar from "../../components/tabbar.vue";
// 导入首页 网络请求
import { getSliders, homeCategory, getFloorList } from "../../api";
export default {
  data () {
    return {
      // 轮播数据
      sliders: [],
      // 首页 分类数据,
      categoryList: [],
      floorList: [],
      headerActive: false,
    };
  },
  components: {
    tabbar,
  },
  mounted () {
    // 页面元素 渲染完成
    // 添加滚动事件
    window.addEventListener("scroll", this.pageScroll);
  },
  created () {
    // 获取首页 初始化数据
    this.getInitdata();
  },
  methods: {
    // 页面 滚动之后 触发的方法
    pageScroll () {
      // console.log("------");
      // 获取滚动距离
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      // console.log("window.pageYOffset:", window.pageYOffset);
      // console.log(
      //   "document.documentElement.scrollTop:",
      //   document.documentElement.scrollTop
      // );
      // console.log("document.body.scrollTop:", document.body.scrollTop);
      this.headerActive = scrollTop > 100 ? true : false;
    },
    // 请求轮播数据
    getInitdata () {
      getSliders((res) => {
        // console.log(res);
        this.sliders = res.data;
      });
      // 请求首页 分类数据
      homeCategory((res) => {
        // console.log("首页分类数据：", res);
        this.categoryList = res.data;
      });
      // 首页 楼梯商品数据
      getFloorList((res) => {
        // console.log(res);
        this.floorList = res.data;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../common/style/mixin.scss";

.home-header {
  position: fixed;
  left: 0;
  top: 0;
  @include fj;
  width: 100%;
  height: 100px;
  line-height: 100px;
  padding: 0 30px;
  @include boxSizing;
  font-size: 30px;
  color: #fff;
  z-index: 10000;
  // background: $red;

  .icon-caidan {
    font-size: 50px;
  }

  .header-search {
    display: flex;
    width: 74%;
    height: 40px;
    line-height: 40px;
    margin: 20px 0;
    padding: 10px 0;
    color: #232326;
    background: #fff;
    @include borderRadius(40px);

    .app-name {
      padding: 0 20px;
      color: $red;
      font-size: 40px;
      font-weight: bold;
      border-right: 1px solid #666;
    }

    .icon-search {
      padding: 0 20px;
      font-size: 34px;
    }

    .search-title {
      font-size: 24px;
      color: #666;
    }
  }

  .icon-iconyonghu {
    font-size: 44px;
  }
}
.category-list {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  width: 100%;
  padding-bottom: 26px;

  div {
    display: flex;
    flex-direction: column;
    width: 20%;
    text-align: center;

    img {
      width: 80px;
      height: 80px;
      margin: 26px auto 16px auto;
    }
  }
}
.floor-list {
  width: 100%;
  padding-bottom: 100px;

  .floor-head {
    width: 100%;
    height: 80px;
    background: #f6f6f6;
  }

  .floor-content {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    @include boxSizing;

    .floor-category {
      width: 50%;
      padding: 20px;
      border-right: 1px solid #dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      @include boxSizing;

      &:nth-child(2n) {
        border-right: none;
      }

      p {
        font-size: 34px;
        color: #333;

        &:nth-child(2) {
          padding: 10px 0;
          font-size: 26px;
          color: $red;
        }
      }

      .floor-products {
        display: flex;
        justify-content: space-around;
        width: 100%;

        img {
          width: 130px;
          height: 130px;
        }
      }
    }
  }
}
</style>


<style>
/* 
    允许 样式 穿透 修改 组件 内部标签样式
    注意： 一定 限定 外层
*/
/* .van-tabbar-item .iconfont {
  font-size: inherit;
} */
</style>